from data_structs import Tweet
from data_structs import Text
import urllib
import time
from datetime import date
from utilities import json_parser
#https://api.foursquare.com/v2/venues/search?ll=36.586498,-121.900002&intent=checkin&query=Days&client_id=MIHKBOMGNI15IUFPLMO1BRCU0CMYACLDD4GCLLEY1540OMPF&client_secret=SZBWWP2UF1LENFWCVM00XEFTNTR1KWPF4KBGVQ5B2RUOSTYR&v=20130303
#https://api.foursquare.com/v2/venues/4527e21df964a520083b1fe3?client_id=MIHKBOMGNI15IUFPLMO1BRCU0CMYACLDD4GCLLEY1540OMPF&client_secret=SZBWWP2UF1LENFWCVM00XEFTNTR1KWPF4KBGVQ5B2RUOSTYR&v=20130303
# api_controller.add() when call API !!!

def crawl_api(one_tweet, api_controller):
    
    latitude = one_tweet.get_latitude()
    longitude = one_tweet.get_longitude()
    keywords = one_tweet.get_keywords()
    current_date = date.today()
    api_url_part1 = "https://api.foursquare.com/v2/venues/search?ll="
    # no keywords
    api_url_part2_1 = "&intent=checkin"
    # has keywords
    api_url_part2_2 = "&intent=checkin&query="
    api_url_part3 = "&limit=1&client_id=MIHKBOMGNI15IUFPLMO1BRCU0CMYACLDD4GCLLEY1540OMPF" \
    + "&client_secret=SZBWWP2UF1LENFWCVM00XEFTNTR1KWPF4KBGVQ5B2RUOSTYR&v=20130" \
    + str(current_date.month) + str(current_date.day)
    #api_url_part4 = "https://api.foursquare.com/v2/venues/"
    #api_url_part5 = "?client_id=MIHKBOMGNI15IUFPLMO1BRCU0CMYACLDD4GCLLEY1540OMPF" \
    #+ "&client_secret=SZBWWP2UF1LENFWCVM00XEFTNTR1KWPF4KBGVQ5B2RUOSTYR&v=20130" \
    #+ str(current_date.month) + str(current_date.day)
    
    if len(keywords) == 0:
        api_url = api_url_part1 + str(latitude) + ',' + str(longitude) + api_url_part2_1 + api_url_part3 
        api_controller.add()
        try:
            page = urllib.urlopen(api_url)
            page_text = page.read().decode('UTF-8')
        except Exception as e:
            print(e)
            text = Text("API", "")
            return text
        if '"venues":[{"id":"' not in page_text:
            text = Text("API", "")
            return text
        else:
            text = json_parser(page_text)
            if text != None:
                return text
            else:
                text = Text("API", "")
                return text          
    else:
        for key_word in keywords:
            api_url = api_url_part1 + str(latitude) + ',' + str(longitude) + api_url_part2_2 \
            + key_word + api_url_part3
            api_controller.add()
            try:
                page = urllib.urlopen(api_url)
                page_text = page.read().decode('UTF-8')
            except Exception as e:
                print(e)
                continue
            if '"venues":[{"id":"' in page_text:
                text = json_parser(page_text)
                if text != None:
                    return text
                else:
                    text = Text("API", "")
                    return text
        text = Text("API", "")
        return text
    
    
    
